package com.powerbosframework.jdbc.datasource;

import com.powerbosframework.jdbc.datasource.JDBCDataSource;
import com.powerbosframework.jdbc.pool.ConnectionPoolManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 加载数据源实现类
 * 
 * @author youfu.wang
 * @version 1.0
 */
public class DriverManagerDatasource implements JDBCDataSource {
	private ConnectionPoolManager connectionPoolManager = ConnectionPoolManager
			.getInstance();
	private Connection connection;
	private String driverClass;
	private String url;
	private String username;
	private String password;
	private String dialect;
	private String showSql;
	private String useQueryCache;
	private String connectionProviderClass;
	private String minConnections;
	private String maxConnections;

	static Class class$(String s) {
		Class cls = null;
		try {
			cls = Thread.currentThread().getContextClassLoader().loadClass(s);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		if (cls == null) {
			try {
				cls = Class.forName(s);
			} catch (ClassNotFoundException classnotfoundexception) {
				throw new NoClassDefFoundError(classnotfoundexception
						.getMessage());
			}
		}
		return cls;
	}

	public DriverManagerDatasource() {
		connection = null;
	}

	public String getDriverClass() {
		return driverClass;
	}

	public void setDriverClass(String driverClass) {
		this.driverClass = driverClass;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDialect() {
		return dialect;
	}

	public void setDialect(String dialect) {
		this.dialect = dialect;
	}

	public String getShowSql() {
		return showSql;
	}

	public void setShowSql(String showSql) {
		this.showSql = showSql;
	}

	public String getUseQueryCache() {
		return useQueryCache;
	}

	public void setUseQueryCache(String useQueryCache) {
		this.useQueryCache = useQueryCache;
	}

	public String getConnectionProviderClass() {
		return connectionProviderClass;
	}

	public void setConnectionProviderClass(String connectionProviderClass) {
		this.connectionProviderClass = connectionProviderClass;
	}

	public String getMinConnections() {
		return minConnections;
	}

	public void setMinConnections(String minConnections) {
		this.minConnections = minConnections;
	}

	public String getMaxConnections() {
		return maxConnections;
	}

	public void setMaxConnections(String maxConnections) {
		this.maxConnections = maxConnections;
	}

	@Override
	/**
	 * 得到数据库连接
	 * 
	 * @throws SQLException
	 * @return
	 */
	public Connection getConnection() throws SQLException {
		
		if (!"".equals(this.connectionProviderClass)&&this.connectionProviderClass!=null) {
			this.loadConnectionProvider();
			connection = connectionPoolManager.getConnection();
		}				
		if (connection == null) {
			try {
				Class.forName(this.getDriverClass());
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			connection = DriverManager.getConnection(this.url, username,
					password);
			return connection;
		} else {
			return connection;
		}
	}

	private void loadConnectionProvider() {
		try {
			Object obj = class$(connectionProviderClass).newInstance();
			Method method = obj.getClass().getMethod("init", JDBCDataSource.class);
			method.invoke(obj, this);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	@Override
	/**
	 * 关闭数据库连接
	 * 
	 * @throws SQLException
	 */
	public void close() throws SQLException {		
		if (connection != null) {			
			if (connectionPoolManager.isUseConnectionPool()) {				
				connectionPoolManager.freeConnection(connection);
			} else {				
				if (!connection.isClosed())
					connection.close();
			}
			connection = null;
		}
	}
}
